package com.jianke.activity;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.tabs.TabLayout;
import com.jianke.R;
import com.jianke.R2;
import com.lib.app.ARouterPathUtils;
import com.lib.app.EventBusTagUtils;
import com.lib.app.FragmentTag;
import com.lib.bean.Event;
import com.lib.fastkit.utils.animation_deal.AnimationUtil;
import com.lib.fastkit.utils.fragment_deal.FragmentCustomUtils;
import com.lib.ui.activity.BaseAppActivity;
import com.lib.view.navigationbar.NomalNavigationBar;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;
import fragment.AllClassFragment_1;
import fragment.ChooseFragment;
import fragment.ExpandClassFragment;
import fragment.JingTiaoClassFragment;
import fragment.PublicClassFragment;
import fragment.ZhuanJiangClassFragment;

@Route(path = ARouterPathUtils.Tablet_JianKe_JianKeListActivity)
public class JianKeListActivity extends BaseAppActivity {

    @BindView(R2.id.tl_tabs)
    TabLayout tlTabs;
    @BindView(R2.id.vp_content)
    ViewPager vpContent;
    @BindView(R2.id.lin_choose)
    LinearLayout linChoose;

    @BindView(R2.id.lin_chooseFrag)
    LinearLayout linChooseFrag;
    @BindView(R2.id.f_choose)
    FrameLayout fChoose;

    ChooseFragment chooseFragment;

    AllClassFragment_1 allClassFragment_1;

    List<Fragment> fgList;

    public String[] titleArr = {"全部课程", "公开课", "拓展课", "专讲课", "精挑课"};

    @Override
    protected void onCreateView() {

        initTitle();
        initViewPager();
        initTabNormal();

        initChooseFragment();
        linChooseFrag.setVisibility(View.GONE);
    }


    protected void initTitle() {
        NomalNavigationBar navigationBar = new
                NomalNavigationBar.Builder(this)
                .setTitle("尖课列表")
                .builder();

    }

    private void initChooseFragment() {
        chooseFragment = new ChooseFragment();
        FragmentCustomUtils.setFragment(this, R.id.f_choose, chooseFragment, FragmentTag.ChooseFragment);

    }

    @OnClick({R2.id.lin_choose})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.lin_choose) {

            if (allClassFragment_1.getUserVisibleHint()) {
                showToast("推荐列表中不能筛选呦！");
            } else {
                setChooseFrag();
            }
        }
    }

    private void setChooseFrag() {
        switch (linChooseFrag.getVisibility()) {
            case View.VISIBLE: {
                menuHide();

                break;
            }

            case View.GONE: {
                showMenu();

                break;
            }
        }
    }

    private void menuHide() {

        linChooseFrag.setVisibility(View.GONE);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(AnimationUtil.leftToView());
        animationSet.addAnimation(AnimationUtil.Alpha1To0());
        linChooseFrag.startAnimation(animationSet);

        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                linChooseFrag.setBackgroundResource(R.color.alpha_black100);
            }

            @Override
            public void onAnimationEnd(Animation animation) {


            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }

    @Subscriber(tag = EventBusTagUtils.ChooseFragment)
    public void fromPersonalFragment(Event event) {
        switch (event.getEventCode()) {
            case 1: {

                if (linChooseFrag.getVisibility() == View.VISIBLE) {
                    menuHide();
                }
                break;
            }
            case 2: {


                break;
            }

        }


    }

    private void showMenu() {

        linChooseFrag.setVisibility(View.VISIBLE);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(AnimationUtil.rightToView());
        animationSet.addAnimation(AnimationUtil.Alpha0To1());
        linChooseFrag.startAnimation(animationSet);
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                linChooseFrag.setBackgroundResource(R.color.alpha_black60);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jiankelist;
    }

    /**
     * 初始化ViewPager，方便后期与tabLayout关联
     */
    private void initViewPager() {
        if (fgList == null)
            fgList = new ArrayList<>();

        allClassFragment_1 = new AllClassFragment_1();

        fgList.add(allClassFragment_1);
        fgList.add(new PublicClassFragment());
        fgList.add(new ExpandClassFragment());
        fgList.add(new ZhuanJiangClassFragment());
        fgList.add(new JingTiaoClassFragment());

        ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fgList);
        vpContent.setAdapter(pagerAdapter);
        pagerAdapter.notifyDataSetChanged();
    }

    /**
     * 初始化顶部标签
     */
    private void initTabNormal() {
        tlTabs.setupWithViewPager(vpContent);

        tlTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //添加选中Tab的逻辑
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //添加未选中Tab的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //再次选中tab的逻辑
            }
        });

    }


    /**
     * ViewPager的适配器。
     */
    class ViewPagerAdapter extends FragmentPagerAdapter {

        List<Fragment> fragmentList;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleArr[position];
        }
    }
}
